#pragma once
#include "DnsMessageWB.h"
#include "afxmt.h"


typedef struct _DNS_INJECT_CONFIG
{
    UINT32      m_nDataSize;
} DNS_INJECT_CONFIG, *PDNS_INJECT_CONFIG;

typedef struct _TCP_INJECT_OUTPUT
{
    DNS_INJECT_CONFIG   m_stInjectConfig;
    WORD                m_wMessageLength;
    BYTE                m_pData[65534];
} TCP_INJECT_OUTPUT, *PTCP_INJECT_OUTPUT;

typedef struct _UDP_INJECT_OUTPUT
{
    DNS_INJECT_CONFIG   m_stInjectConfig;
    BYTE                m_pData[512];
} UDP_INJECT_OUTPUT, *PUDP_INJECT_OUTPUT;


#define WAIT_ELABORATE_TIMEOUT 40000
// 480000 INFINITE
// CIp4ElaborateThread
class CMainWorkerThread;
class CDnsMessageWB;
class CDnsResorceRecord;
class CIp4ElaborateThread : public CWinThread
{
	DECLARE_DYNCREATE(CIp4ElaborateThread)

public:
	CIp4ElaborateThread();
	virtual ~CIp4ElaborateThread();

public:
	virtual BOOL InitInstance();
	virtual int ExitInstance();

protected:
	DECLARE_MESSAGE_MAP()
public:
    virtual BOOL OnIdle(LONG lCount);
    int m_nRetCode;
    CMainWorkerThread* m_pOwner;
    CEvent m_oThreadInitDone;
    POSITION m_pElaboratedListPositon;
    POSITION m_pAllocatedListPosition;
    HANDLE m_hDnsFilterFile;
    FLOW_DATA m_stFlowData;
    CEvent m_oWaitDnsQueryEvent;
    OVERLAPPED m_stWaitDnsQueryOverlaped;
    OVERLAPPED* m_pWaitDnsQueryOverlaped;
    CEvent m_oWaitFlowEstablishedDoneEvent;
    OVERLAPPED m_stWaitFlowEstablishedOverlapped;
    OVERLAPPED* m_pWaitFlowEstablishedOverlapped;
    bool m_bWaitStarted;
    IPPROTO m_enIpProtocol;
    BYTE m_baDnsQueryBuffer[0x10000];
    BYTE m_baDnsResponceBuffer[0x10000];
    bool m_bActive;
    DNS_APPROVAL_OUTPUT m_stDnsApprovalOutput;
    CDnsMessageWB* m_pDnsQueryMessage;
    bool m_bAbortedQuery;
    CDnsMessageWB* m_pZoneTransferMessage;
    bool ReCreateZoneTransferMessage(void);
    void GetPtrAddressesInfo(
        CDnsResorceRecord* pSourceRR,
        CString sTargetDName,
        int& rA4Count,
        int& rA4PrivateCount,
        int& rA6Count,
        int& rA6PrivateCount
        );
    TCP_INJECT_OUTPUT m_stTcpInjectOutput;
    CEvent m_oThreadExitDone;
    bool m_bFlowEstablished;
    bool m_bThreadSemaforeLocked;
    CString m_sDnsFilterFileName;
    OVERLAPPED m_stWaitUdpResponceOverlapped;
    OVERLAPPED* m_pWaitUdpResponceOverlapped;
    CEvent m_oWaitUdpResponceDoneEvent;
    OVERLAPPED m_stWaitTcpResponceOverlapped;
    OVERLAPPED* m_pWaitTcpResponceOverlapped;
    CEvent m_oWaitTcpResponceDoneEvent;
    OVERLAPPED m_stWaitTcpNewMessageOverlapped;
    OVERLAPPED* m_pWaitTcpNewMessageOverlapped;
    CEvent m_oWaitTcpNewMessageDoneEvent;
    OVERLAPPED m_stWaitFlowDoneOverlaped;
    OVERLAPPED* m_pWaitFlowDoneOverlaped;
    CEvent m_oWaitFlowDoneEvent;
    bool m_bIPv6;
    DWORD m_dwOriginalResponceUDP;
//    virtual BOOL PreTranslateMessage(MSG* pMsg);
//    virtual BOOL ProcessMessageFilter(int code, LPMSG lpMsg);
};
